﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EpiNEXT.Applications;

namespace EpiNEXT.Presentation.Enter
{
    /// <summary>
    /// Interaction logic for EnterWindow.xaml
    /// </summary>
    public partial class EnterWindow : Window
    {
        public EnterWindow()
        {
            InitializeComponent();
            //FirstFloor.ModernUI.Presentation.AppearanceManager.Current.DarkThemeCommand.Execute(this);
            //FirstFloor.ModernUI.Presentation.AppearanceManager.Current.AccentColorCommand.Execute(Colors.CornflowerBlue);
            //FirstFloor.ModernUI.Presentation.AppearanceManager.Current.AccentColorCommand.Execute(Color.FromRgb(154, 29, 32)); // red accent
            FirstFloor.ModernUI.Presentation.AppearanceManager.Current.AccentColorCommand.Execute(Color.FromRgb(0, 122, 204));
            printViewer.DataContext = this.DataContext;
        }

        private void Forms_BackButtonClicked(object sender, EventArgs args)
        {
            pageForms.Visibility = System.Windows.Visibility.Collapsed;
            pageHome.Visibility = System.Windows.Visibility.Visible;

            grdGettingStarted.Visibility = System.Windows.Visibility.Visible;
            // TODO: Move this to custom controls
        }

        private void Forms_FormOpened(object sender, Applications.Events.FormOpenedEventArgs args)
        {
            //grdIntro.Visibility = System.Windows.Visibility.Collapsed;
            ProjectViewModel vm = this.DataContext as ProjectViewModel;
            vm.LoadFormCommand.Execute(args.FormInfo);
            // TODO: Move this to custom controls
        }

        private void Home_ProjectOpened(object sender, Applications.Events.ProjectOpenedEventArgs args)
        {
            pageHome.Visibility = System.Windows.Visibility.Collapsed;
            pageForms.DataContext = args.ProjectInfo;
            pageForms.Visibility = System.Windows.Visibility.Visible;

            grdGettingStarted.Visibility = System.Windows.Visibility.Collapsed;
            // TODO: Move this to custom controls
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            ProjectViewModel vm = this.DataContext as ProjectViewModel;

            if (vm.HasUnsavedChanges)
            {
                MessageBoxResult result = MessageBox.Show("There are unsaved changes in this project. Save before closing?",
                    "Save?", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

                if (result == MessageBoxResult.Cancel)
                {
                    e.Cancel = true;
                }
                else if (result == MessageBoxResult.Yes)
                {
                    if (vm.SaveRecordCommand.CanExecute(null))
                    {
                        this.Cursor = Cursors.Wait;
                        vm.SaveRecordCommand.Execute(null);
                        this.Cursor = Cursors.Arrow;
                    }
                    else
                    {
                        MessageBoxResult resultCanSave = MessageBox.Show("Changes cannot be saved; please make sure there are no validation errors and try again.",
                            "Cannot save", MessageBoxButton.OK, MessageBoxImage.Error);
                        e.Cancel = true;
                    }
                }
            }
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.AutoUpgradeEnabled = true;
            ofd.CheckFileExists = true;

            ofd.DefaultExt = "xml";
            ofd.Filter = "Epi Info 7 Project Template (*.xml)|*.xml";

            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            System.Windows.Forms.DialogResult result = ofd.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                //EpiNEXT.Core.Legacy.EI7ProjectTemplateConverter converter = new Core.Legacy.EI7ProjectTemplateConverter(ofd.FileName);
                //Project project = converter.Convert();

                ProjectViewModel vm = (this.DataContext as ProjectViewModel);
                vm.UpgradeTemplateCommand.Execute(ofd.FileName);
            }
        }
    }
}
